package com.j256.ormlite.harmony;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.UnknownHostException;
import java.util.Locale;

/**
 * Log utils
 * Harmony 平台Log 工具类
 */
public class Log {
    private static final String TAG_LOG = "[] ";

    private static final int DOMAIN_ID = 0xD000F00;

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, Log.TAG_LOG);

    private static final String LOG_FORMAT = "%s: %s";

    private Log() {
        /* Do nothing */
    }

    public static boolean isLoggable(String tag, int level) {
        return HiLog.isLoggable(DOMAIN_ID, tag, level);
    }

    public static String getStackTraceString(Throwable tr) {
        if (tr == null) {
            return "";
        }

        // This is to reduce the amount of log spew that apps do in the non-error
        // condition of the network being unavailable.
        Throwable t = tr;
        while (t != null) {
            if (t instanceof UnknownHostException) {
                return "";
            }
            t = t.getCause();
        }

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        tr.printStackTrace(pw);
        pw.flush();
        return sw.toString();
    }

    /**
     * Print debug log
     *
     * @param tag log tag
     * @param msg log message
     */
    public static void d(String tag, String msg) {
        HiLog.debug(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
    }

    public static int d(String tag, String msg, Throwable tr) {
        return HiLog.debug(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg + '\n' + getStackTraceString(tr)));
    }

    /**
     * Print info log
     *
     * @param tag log tagcc
     * @param msg log message
     */
    public static void i(String tag, String msg) {
        HiLog.info(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
    }

    public static int i(String tag, String msg, Throwable tr) {
        return HiLog.info(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg + '\n' + getStackTraceString(tr)));
    }

    /**
     * Print warn log
     *
     * @param tag log tag
     * @param msg log message
     */
    public static void w(String tag, String msg) {
        HiLog.warn(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
    }

    public static int w(String tag, String msg, Throwable tr) {
        return HiLog.warn(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg + '\n' + getStackTraceString(tr)));
    }

    /**
     * Print error log
     *
     * @param tag log tag
     * @param msg log message
     */
    public static void e(String tag, String msg) {
        HiLog.error(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
    }

    public static void e(String tag, String msg, Throwable tr) {
        HiLog.error(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg + '\n' + getStackTraceString(tr)));
    }
}